package cn.dxc.demo.controller;

import cn.dxc.demo.common.RegxUtils;
import cn.dxc.demo.common.ResultMapUtil;
import cn.dxc.demo.entity.FeedBack;
import cn.dxc.demo.service.IBackService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "用户反馈")
public class FeedBackController {

    @Autowired
    private IBackService backService;

    @PostMapping("/feedBack")
    @ApiOperation("反馈详情")
    @ApiImplicitParams({@ApiImplicitParam(value = "反馈id",name = "id", required = false, dataType = "int", paramType = "query",defaultValue="1"),
            @ApiImplicitParam(value = "用户id",name = "uid",required = true,dataType = "int",paramType = "query",defaultValue = "1"),
            @ApiImplicitParam(value = "反馈详情",name = "feedbackContent",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "反馈邮箱",name = "email",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "反馈明细",name = "contact_details",required = true,dataType = "String",paramType = "query")})
    public Object feedBack(FeedBack feedBack){

        if(RegxUtils.isEmail(feedBack.getEmail())==false){
            return ResultMapUtil.getHashMapInfo(null,"0","邮箱格式错误！");
        }

        try{
            final Integer flag = backService.insertMessage(feedBack);
            if (flag == 0){
                return ResultMapUtil.getHashMapInfo(null,"0","失败！");
            }else {
                try{
                    final Integer integer = backService.updateUserEmail(feedBack.getUid(), feedBack.getEmail());
                    if (integer == 1){
                        return ResultMapUtil.getHashMapInfo(null,"1","成功！");
                    }
                }catch (DataAccessException e){
                    return ResultMapUtil.getHashMapInfo("数据库异常","0","失败！");
                }
                return ResultMapUtil.getHashMapInfo(null,"0","失败！");
            }
        }catch (DataAccessException e){
            return ResultMapUtil.getHashMapInfo("数据库异常","0","失败！");

        }
    }
}
